package www.dianbaozu.com.ui.my.personalHomepage

import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import www.wen.com.common.lib.base.module.BaseApiViewModel
import www.wen.com.common.lib.data.entities.*
import www.wen.com.common.lib.data.repository.AppDataManager

class PersonalHomepageViewModel @ViewModelInject constructor(appDataManager: AppDataManager) :
    BaseApiViewModel(appDataManager) {

    @FlowPreview
    fun requestUserInfoById(
        id: String,
        action: ((Result.Success<UseInfoEntity>) -> Unit)
    ) {
        viewModelScope.launch {
            appDataManager.requestUserInfoByIdFlow(id)
                .catch { cause ->
                    loading.value = UiStateCustom(
                        stateData = LoadingDialog(
                            tip = cause.message ?: "网络异常",
                            loadingDialogStateType = LoadingDialogStateType.ERROR,
                            isShowDialogTip = false,
                            isShowToastTip = true
                        )
                    )
                }
                .collect {
                    action.invoke(it)
                }
        }
    }
}